package com.xiaoshou.dao;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.xiaoshou.pojo.Answers;
@Repository
public class AnswersDao extends BaseDao<Answers, String>{

	public void saveNewAnswers(Answers answers) {
		String sql = "INSERT INTO answers VALUES(:id,:content,:state,:createtime,:updatetime,:lastexamine,:questionid,:userid)";
		
		Query query = getSession().createSQLQuery(sql);
		query.setParameter("id", answers.getId());
		query.setText("content", answers.getContent());
		query.setParameter("state", answers.getState());
		query.setParameter("questionid", answers.getQuestion().getId());
		query.setParameter("userid", answers.getUser().getId());
		query.setParameter("createtime", answers.getCreatetime());
		query.setParameter("updatetime", answers.getUpdatetime());
		query.setParameter("lastexamine", answers.getUser().getUsername());
		query.executeUpdate();
	}

	public void updateAnswer(Answers answers) {
		String sql = "";
		if(answers.getLastexamine()!=null){
			sql = "UPDATE answers set content=:content,state=:state,updatetime=:updatetime,lastexamine=:lastexamine where id=:id";
		}else {
			sql = "UPDATE answers set content=:content,updatetime=:updatetime,state=:state  where id=:id";
		}
		Query query = getSession().createSQLQuery(sql);
		query.setParameter("content", answers.getContent());
		query.setParameter("updatetime", answers.getUpdatetime());
		query.setParameter("state", answers.getState());
		if(answers.getLastexamine()!=null){
			query.setParameter("lastexamine", answers.getLastexamine());
		}
		query.setParameter("id", answers.getId());
		query.executeUpdate();
	}

	public void deleteAnswer(String answerId) {
		String sql = "delete from answers where id=:id";
		Query query = getSession().createSQLQuery(sql);
		query.setParameter("id", answerId);
		query.executeUpdate();
	}

}
